* convert geojson format to Format class.
use gpsbable::File instead of QFile for input. This results in
better error messages.
use gpsbabel::File for output instead of gbfile.
* detect and log parser errors in geojson reader.
gbser.h
gbser_private.h
gbversion.h
+ geojson.h
ggv_bin.h
gpx.h
grtcirc.h
gbser.h \
gbser_private.h \
gbversion.h \
+ geojson.h \
ggv_bin.h \
gpx.h \
grtcirc.h \
duplicate.h height.h heightgrid.h interpolate.h nukedata.h polygon.h \
position.h radius.h reverse_route.h smplrout.h sort.h stackfilter.h \
swapdata.h trackfilter.h transform.h validate.h gbversion.h vecs.h \
- format.h ggv_bin.h gpx.h src/core/file.h src/core/xmlstreamwriter.h \
- src/core/xmltag.h shape.h shapelib/shapefil.h yahoo.h xmlgeneric.h \
- legacyformat.h mynav.h xcsv.h garmin_fs.h jeeps/gps.h jeeps/../defs.h \
- jeeps/gpsport.h jeeps/gpsdevice.h jeeps/gpssend.h jeeps/gpsread.h \
- jeeps/gpsutil.h jeeps/gpsapp.h jeeps/gpsprot.h jeeps/gpscom.h \
- jeeps/gpsfmt.h jeeps/gpsmath.h jeeps/gpsmem.h jeeps/gpsrqst.h \
- src/core/textstream.h
+ format.h geojson.h src/core/file.h ggv_bin.h gpx.h \
+ src/core/xmlstreamwriter.h src/core/xmltag.h legacyformat.h mynav.h \
+ shape.h shapelib/shapefil.h xcsv.h garmin_fs.h jeeps/gps.h \
+ jeeps/../defs.h jeeps/gpsport.h jeeps/gpsdevice.h jeeps/gpssend.h \
+ jeeps/gpsread.h jeeps/gpsutil.h jeeps/gpsapp.h jeeps/gpsprot.h \
+ jeeps/gpscom.h jeeps/gpsfmt.h jeeps/gpsmath.h jeeps/gpsmem.h \
+ jeeps/gpsrqst.h src/core/textstream.h yahoo.h xmlgeneric.h
formspec.o: formspec.cc defs.h config.h zlib/zlib.h zlib/zconf.h \
formspec.h inifile.h gbfile.h session.h src/core/datetime.h \
src/core/optional.h
jeeps/gpsread.h jeeps/gpsutil.h jeeps/gpsapp.h jeeps/gpsprot.h \
jeeps/gpscom.h jeeps/gpsfmt.h jeeps/gpsmath.h jeeps/gpsmem.h \
jeeps/gpsrqst.h garmin_tables.h grtcirc.h jeeps/gpsserial.h vecs.h \
- ggv_bin.h gpx.h src/core/file.h src/core/xmlstreamwriter.h \
- src/core/xmltag.h shape.h shapelib/shapefil.h yahoo.h xmlgeneric.h \
- legacyformat.h mynav.h xcsv.h src/core/textstream.h
+ geojson.h src/core/file.h ggv_bin.h gpx.h src/core/xmlstreamwriter.h \
+ src/core/xmltag.h legacyformat.h mynav.h shape.h shapelib/shapefil.h \
+ xcsv.h src/core/textstream.h yahoo.h xmlgeneric.h
garmin_device_xml.o: garmin_device_xml.cc defs.h config.h zlib/zlib.h \
zlib/zconf.h formspec.h inifile.h gbfile.h session.h \
src/core/datetime.h src/core/optional.h garmin_device_xml.h \
src/core/file.h
geojson.o: geojson.cc defs.h config.h zlib/zlib.h zlib/zconf.h formspec.h \
inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h \
- src/core/file.h
+ geojson.h format.h src/core/file.h
ggv_bin.o: ggv_bin.cc ggv_bin.h defs.h config.h zlib/zlib.h zlib/zconf.h \
formspec.h inifile.h gbfile.h session.h src/core/datetime.h \
src/core/optional.h format.h
magproto.o: magproto.cc defs.h config.h zlib/zlib.h zlib/zconf.h \
formspec.h inifile.h gbfile.h session.h src/core/datetime.h \
src/core/optional.h explorist_ini.h format.h gbser.h magellan.h vecs.h \
- ggv_bin.h gpx.h src/core/file.h src/core/xmlstreamwriter.h \
- src/core/xmltag.h shape.h shapelib/shapefil.h yahoo.h xmlgeneric.h \
- legacyformat.h mynav.h xcsv.h garmin_fs.h jeeps/gps.h jeeps/../defs.h \
- jeeps/gpsport.h jeeps/gpsdevice.h jeeps/gpssend.h jeeps/gpsread.h \
- jeeps/gpsutil.h jeeps/gpsapp.h jeeps/gpsprot.h jeeps/gpscom.h \
- jeeps/gpsfmt.h jeeps/gpsmath.h jeeps/gpsmem.h jeeps/gpsrqst.h \
- src/core/textstream.h
+ geojson.h src/core/file.h ggv_bin.h gpx.h src/core/xmlstreamwriter.h \
+ src/core/xmltag.h legacyformat.h mynav.h shape.h shapelib/shapefil.h \
+ xcsv.h garmin_fs.h jeeps/gps.h jeeps/../defs.h jeeps/gpsport.h \
+ jeeps/gpsdevice.h jeeps/gpssend.h jeeps/gpsread.h jeeps/gpsutil.h \
+ jeeps/gpsapp.h jeeps/gpsprot.h jeeps/gpscom.h jeeps/gpsfmt.h \
+ jeeps/gpsmath.h jeeps/gpsmem.h jeeps/gpsrqst.h src/core/textstream.h \
+ yahoo.h xmlgeneric.h
main.o: main.cc defs.h config.h zlib/zlib.h zlib/zconf.h formspec.h \
inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h \
cet_util.h csv_util.h filter.h filter_vecs.h arcdist.h bend.h \
discard.h duplicate.h height.h heightgrid.h interpolate.h nukedata.h \
polygon.h position.h radius.h reverse_route.h smplrout.h sort.h \
stackfilter.h swapdata.h trackfilter.h transform.h validate.h format.h \
- src/core/file.h src/core/usasciicodec.h vecs.h ggv_bin.h gpx.h \
- src/core/xmlstreamwriter.h src/core/xmltag.h shape.h \
- shapelib/shapefil.h yahoo.h xmlgeneric.h legacyformat.h mynav.h xcsv.h \
- garmin_fs.h jeeps/gps.h jeeps/../defs.h jeeps/gpsport.h \
- jeeps/gpsdevice.h jeeps/gpssend.h jeeps/gpsread.h jeeps/gpsutil.h \
- jeeps/gpsapp.h jeeps/gpsprot.h jeeps/gpscom.h jeeps/gpsfmt.h \
- jeeps/gpsmath.h jeeps/gpsmem.h jeeps/gpsrqst.h src/core/textstream.h
+ src/core/file.h src/core/usasciicodec.h vecs.h geojson.h ggv_bin.h \
+ gpx.h src/core/xmlstreamwriter.h src/core/xmltag.h legacyformat.h \
+ mynav.h shape.h shapelib/shapefil.h xcsv.h garmin_fs.h jeeps/gps.h \
+ jeeps/../defs.h jeeps/gpsport.h jeeps/gpsdevice.h jeeps/gpssend.h \
+ jeeps/gpsread.h jeeps/gpsutil.h jeeps/gpsapp.h jeeps/gpsprot.h \
+ jeeps/gpscom.h jeeps/gpsfmt.h jeeps/gpsmath.h jeeps/gpsmem.h \
+ jeeps/gpsrqst.h src/core/textstream.h yahoo.h xmlgeneric.h
mapasia.o: mapasia.cc defs.h config.h zlib/zlib.h zlib/zconf.h formspec.h \
inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h
mapbar_track.o: mapbar_track.cc defs.h config.h zlib/zlib.h zlib/zconf.h \
gbser.h
smplrout.o: smplrout.cc defs.h config.h zlib/zlib.h zlib/zconf.h \
formspec.h inifile.h gbfile.h session.h src/core/datetime.h \
- src/core/optional.h grtcirc.h smplrout.h filter.h
+ src/core/optional.h smplrout.h filter.h grtcirc.h
sort.o: sort.cc defs.h config.h zlib/zlib.h zlib/zconf.h formspec.h \
inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h \
sort.h filter.h
jeeps/gpsmath.h jeeps/gpsport.h
vecs.o: vecs.cc defs.h config.h zlib/zlib.h zlib/zconf.h formspec.h \
inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h \
- vecs.h format.h ggv_bin.h gpx.h src/core/file.h \
- src/core/xmlstreamwriter.h src/core/xmltag.h shape.h \
- shapelib/shapefil.h yahoo.h xmlgeneric.h legacyformat.h mynav.h xcsv.h \
- garmin_fs.h jeeps/gps.h jeeps/../defs.h jeeps/gpsport.h \
- jeeps/gpsdevice.h jeeps/gpssend.h jeeps/gpsread.h jeeps/gpsutil.h \
- jeeps/gpsapp.h jeeps/gpsprot.h jeeps/gpscom.h jeeps/gpsfmt.h \
- jeeps/gpsmath.h jeeps/gpsmem.h jeeps/gpsrqst.h src/core/textstream.h \
- gbversion.h src/core/logging.h
+ vecs.h format.h geojson.h src/core/file.h ggv_bin.h gpx.h \
+ src/core/xmlstreamwriter.h src/core/xmltag.h legacyformat.h mynav.h \
+ shape.h shapelib/shapefil.h xcsv.h garmin_fs.h jeeps/gps.h \
+ jeeps/../defs.h jeeps/gpsport.h jeeps/gpsdevice.h jeeps/gpssend.h \
+ jeeps/gpsread.h jeeps/gpsutil.h jeeps/gpsapp.h jeeps/gpsprot.h \
+ jeeps/gpscom.h jeeps/gpsfmt.h jeeps/gpsmath.h jeeps/gpsmem.h \
+ jeeps/gpsrqst.h src/core/textstream.h yahoo.h xmlgeneric.h gbversion.h \
+ src/core/logging.h
vidaone.o: vidaone.cc defs.h config.h zlib/zlib.h zlib/zconf.h formspec.h \
inifile.h gbfile.h session.h src/core/datetime.h src/core/optional.h
vitosmt.o: vitosmt.cc defs.h config.h zlib/zlib.h zlib/zconf.h formspec.h \
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-#include "defs.h"
-#include "src/core/file.h"
-#include <QtCore/QJsonArray>
-#include <QtCore/QJsonDocument>
-#include <QtCore/QJsonObject>
-static gbfile* ofd;
-static QString input_file_name;
-static const char MYNAME[] = "geojson";
-static char* compact_opt = nullptr;
-static QJsonObject* track_object = nullptr;
-static QJsonArray* track_coords = nullptr;
+#include <QtCore/QByteArray> // for QByteArray
+#include <QtCore/QIODevice> // for operator|, QIODevice, QIODevice::ReadOnly, QIODevice::Text
+#include <QtCore/QJsonArray> // for QJsonArray
+#include <QtCore/QJsonDocument> // for QJsonDocument, QJsonDocument::Compact, QJsonDocument::Indented, QJsonDocument::JsonFormat
+#include <QtCore/QJsonObject> // for QJsonObject
+#include <QtCore/QJsonParseError> // for QJsonParseError
+#include <QtCore/QJsonValue> // for QJsonValue
+#include <QtCore/QJsonValueRef> // for QJsonValueRef
-static const QString FEATURE_COLLECTION = QStringLiteral("FeatureCollection");
-static const QString FEATURE = QStringLiteral("Feature");
-static const QString POINT = QStringLiteral("Point");
-static const QString MULTIPOINT = QStringLiteral("MultiPoint");
-static const QString LINESTRING = QStringLiteral("LineString");
-static const QString MULTILINESTRING = QStringLiteral("MultiLineString");
-static const QString POLYGON = QStringLiteral("Polygon");
-static const QString MULTIPOLYGON = QStringLiteral("MultiPolygon");
-static const QString TYPE = QStringLiteral("type");
-static const QString FEATURES = QStringLiteral("features");
-static const QString COORDINATES = QStringLiteral("coordinates");
-static const QString GEOMETRY = QStringLiteral("geometry");
-static const QString PROPERTIES = QStringLiteral("properties");
-static const QString NAME = QStringLiteral("name");
-static const QString DESCRIPTION = QStringLiteral("description");
-static const QString URL = QStringLiteral("url");
-static const QString URLNAME = QStringLiteral("urlname");
+#include "defs.h"
+#include "geojson.h"
+#include "src/core/file.h" // for File
+#include "src/core/logging.h" // for Fatal
-static QVector<arglist_t> geojson_args = {
- {"compact", &compact_opt, "Compact Output. Default is off.",
- nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr } ,
-};
-static void
-geojson_rd_init(const QString& fname) {
- input_file_name = fname;
+void
+GeoJsonFormat::rd_init(const QString& fname) {
+ ifd = new gpsbabel::File(fname);
+ ifd->open(QIODevice::ReadOnly | QIODevice::Text);
}
-static QJsonArray* feature_collection = nullptr;
-
-static void
-geojson_wr_init(const QString& fname) {
+void
+GeoJsonFormat::wr_init(const QString& fname) {
feature_collection = new QJsonArray;
- ofd = gbfopen(fname, "w", MYNAME);
+ ofd = new gpsbabel::File(fname);
+ ofd->open(QIODevice::WriteOnly);
}
-static void
-geojson_waypt_pr(const Waypoint* waypoint) {
+void
+GeoJsonFormat::geojson_waypt_pr(const Waypoint* waypoint) const
+{
QJsonObject geometry;
geometry[TYPE] = POINT;
QJsonArray coordinates;
feature_collection->append(feature);
}
-static void
-geojson_rd_deinit() {
- gbfclose(ofd);
- ofd = nullptr;
+void
+GeoJsonFormat::rd_deinit() {
+ ifd->close();
+ delete ifd;
+ ifd = nullptr;
}
-static void
-geojson_wr_deinit() {
+void
+GeoJsonFormat::wr_deinit() {
QJsonObject object;
object[TYPE] = FEATURE_COLLECTION;
object[FEATURES] = *feature_collection;
QJsonDocument save(object);
QJsonDocument::JsonFormat style = compact_opt ? QJsonDocument::Compact : QJsonDocument::Indented;
- gbfputs(save.toJson(style),ofd);
+ ofd->write(save.toJson(style));
+ ofd->close();
- gbfclose(ofd);
+ delete ofd;
ofd = nullptr;
delete feature_collection;
feature_collection = nullptr;
}
-static Waypoint*
-waypoint_from_coordinates(const QJsonArray& coordinates)
+Waypoint*
+GeoJsonFormat::waypoint_from_coordinates(const QJsonArray& coordinates)
{
auto waypoint = new Waypoint();
waypoint->latitude = coordinates.at(1).toDouble();
return waypoint;
}
-static void
-routes_from_polygon_coordinates(const QJsonArray& polygon)
+void
+GeoJsonFormat::routes_from_polygon_coordinates(const QJsonArray& polygon)
{
for (auto && lineStringIterator : polygon)
{
}
}
-static void
-geojson_read() {
- QFile file;
- file.setFileName(input_file_name);
- file.open(QIODevice::ReadOnly | QIODevice::Text);
- QString file_content = file.readAll();
- file.close();
+void
+GeoJsonFormat::read() {
+ QString file_content = ifd->readAll();
QJsonParseError error{};
QJsonDocument document = QJsonDocument::fromJson(file_content.toUtf8(), &error);
+ if (error.error != QJsonParseError::NoError) {
+ Fatal().nospace() << MYNAME << ": GeoJSON parse error in " << ifd->fileName() << ": " << error.errorString();
+ }
QJsonObject rootObject = document.object();
if (rootObject[TYPE] != FEATURE_COLLECTION)
}
-static void geojson_track_hdr(const route_head* track) {
+void GeoJsonFormat::geojson_track_hdr(const route_head* track) {
track_object = new QJsonObject();
(*track_object)[TYPE] = FEATURE;
(*track_object)[PROPERTIES] = properties;
}
-static void geojson_track_disp(const Waypoint* trackpoint) {
+void GeoJsonFormat::geojson_track_disp(const Waypoint* trackpoint) const
+{
QJsonArray coords;
coords.append(trackpoint->longitude);
(*track_coords).append(coords);
}
-static void geojson_track_tlr(const route_head*) {
+void GeoJsonFormat::geojson_track_tlr(const route_head* /*unused*/) {
QJsonObject geometry;
geometry[TYPE] = LINESTRING;
geometry[COORDINATES] = *track_coords;
track_coords = nullptr;
}
-static void
-geojson_write() {
- waypt_disp_all(geojson_waypt_pr);
- track_disp_all(geojson_track_hdr, geojson_track_tlr, geojson_track_disp);
+void
+GeoJsonFormat::write() {
+ auto geojson_waypt_pr_lambda = [this](const Waypoint* waypointp)->void {
+ geojson_waypt_pr(waypointp);
+ };
+ waypt_disp_all(geojson_waypt_pr_lambda);
+
+ auto geojson_track_hdr_lambda = [this](const route_head* rte)->void {
+ geojson_track_hdr(rte);
+ };
+ auto geojson_track_tlr_lambda = [this](const route_head* rte)->void {
+ geojson_track_tlr(rte);
+ };
+ auto geojson_track_disp_lambda = [this](const Waypoint* waypointp)->void {
+ geojson_track_disp(waypointp);
+ };
+ track_disp_all(geojson_track_hdr_lambda, geojson_track_tlr_lambda, geojson_track_disp_lambda);
}
-ff_vecs_t geojson_vecs = {
- ff_type_file,
- {
- (ff_cap)(ff_cap_read | ff_cap_write) /* waypoints */,
- (ff_cap)(ff_cap_read | ff_cap_write) /* tracks */,
- (ff_cap)(ff_cap_read | ff_cap_write) /* routes */,
- },
- geojson_rd_init,
- geojson_wr_init,
- geojson_rd_deinit,
- geojson_wr_deinit,
- geojson_read,
- geojson_write,
- nullptr,
- &geojson_args,
- CET_CHARSET_UTF8, 0 /* CET-REVIEW */
- , NULL_POS_OPS,
- nullptr
-};
--- /dev/null
+/*
+ Copyright (C) 2016 Robert Lipe, robertlipe+source@gpsbabel.org
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ */
+#ifndef GEOJSON_H_INCLUDED_
+#define GEOJSON_H_INCLUDED_
+
+#include <QtCore/QJsonArray> // for QJsonArray
+#include <QtCore/QJsonObject> // for QJsonObject
+#include <QtCore/QStaticStringData> // for QStaticStringData
+#include <QtCore/QString> // for QString, QStringLiteral
+#include <QtCore/QVector> // for QVector
+
+#include "defs.h"
+#include "format.h" // for Format
+#include "src/core/file.h"
+
+class GeoJsonFormat : public Format
+{
+public:
+ QVector<arglist_t>* get_args() override
+ {
+ return &geojson_args;
+ }
+
+ ff_type get_type() const override
+ {
+ return ff_type_file;
+ }
+
+ QVector<ff_cap> get_cap() const override
+ {
+ return FF_CAP_RW_ALL;
+ }
+
+ QString get_encode() const override
+ {
+ return CET_CHARSET_UTF8;
+ }
+
+ int get_fixed_encode() const override
+ {
+ return 0;
+ }
+
+ void rd_init(const QString& fname) override;
+ void read() override;
+ void rd_deinit() override;
+ void wr_init(const QString& fname) override;
+ void write() override;
+ void wr_deinit() override;
+
+ void geojson_waypt_pr(const Waypoint* waypoint) const;
+ static Waypoint* waypoint_from_coordinates(const QJsonArray& coordinates);
+ void routes_from_polygon_coordinates(const QJsonArray& polygon);
+ void geojson_track_hdr(const route_head* track);
+ void geojson_track_disp(const Waypoint* trackpoint) const;
+ void geojson_track_tlr(const route_head* unused);
+
+private:
+gpsbabel::File* ifd{nullptr};
+gpsbabel::File* ofd{nullptr};
+const char* MYNAME = "geojson";
+char* compact_opt = nullptr;
+QJsonObject* track_object = nullptr;
+QJsonArray* track_coords = nullptr;
+
+const QString FEATURE_COLLECTION = QStringLiteral("FeatureCollection");
+const QString FEATURE = QStringLiteral("Feature");
+const QString POINT = QStringLiteral("Point");
+const QString MULTIPOINT = QStringLiteral("MultiPoint");
+const QString LINESTRING = QStringLiteral("LineString");
+const QString MULTILINESTRING = QStringLiteral("MultiLineString");
+const QString POLYGON = QStringLiteral("Polygon");
+const QString MULTIPOLYGON = QStringLiteral("MultiPolygon");
+const QString TYPE = QStringLiteral("type");
+const QString FEATURES = QStringLiteral("features");
+const QString COORDINATES = QStringLiteral("coordinates");
+const QString GEOMETRY = QStringLiteral("geometry");
+const QString PROPERTIES = QStringLiteral("properties");
+const QString NAME = QStringLiteral("name");
+const QString DESCRIPTION = QStringLiteral("description");
+const QString URL = QStringLiteral("url");
+const QString URLNAME = QStringLiteral("urlname");
+
+QVector<arglist_t> geojson_args = {
+ {"compact", &compact_opt, "Compact Output. Default is off.",
+ nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr } ,
+};
+
+QJsonArray* feature_collection = nullptr;
+
+};
+#endif // GEOJSON_H_INCLUDED_
#include "defs.h"
#include "format.h"
+#include "geojson.h"
#include "ggv_bin.h"
#include "gpx.h"
#include "legacyformat.h"
extern ff_vecs_t f90g_track_vecs;
extern ff_vecs_t mapfactor_vecs;
extern ff_vecs_t energympro_vecs;
-extern ff_vecs_t geojson_vecs;
extern ff_vecs_t globalsat_sport_vecs;
#endif // MAXIMAL_ENABLED
LegacyFormat mapfactor_fmt {mapfactor_vecs};
LegacyFormat energympro_fmt {energympro_vecs};
MyNavFormat mynav_fmt;
- LegacyFormat geojson_fmt {geojson_vecs};
+ GeoJsonFormat geojson_fmt;
GgvBinFormat ggv_bin_fmt;
LegacyFormat globalsat_sport_fmt {globalsat_sport_vecs};
#endif // MAXIMAL_ENABLED